package com.mall.ums.controller;

import com.mall.model.domain.ums.Menu;
import com.mall.model.domain.vo.MenuVo;
import com.mall.ums.service.MenuService;
import com.mall.utils.PageResult;
import com.mall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author clutchcitylee
 * @create 2023-03-13-14:03
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 添加后台菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody Menu menu) {
        menu.setCreateTime(new Date());
        menuService.save(menu);
        return R.success("添加成功");
    }

    /**
     * 根据ID删除后台菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R<String> deleteById(@PathVariable Integer id) {
        menuService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 分页查询后台菜单
     *
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{parentId}")
    public R<PageResult> list(@PathVariable Integer parentId, Integer pageNum, Integer pageSize) {
        PageResult pageResult = menuService.listByParentId(parentId, pageNum, pageSize);
        return R.success(pageResult);
    }

    /**
     * 树形结构返回所有菜单列表
     *
     * @return
     */
    @GetMapping("/treeList")
    public R<List<MenuVo>> treeList() {
        List<MenuVo> vos = menuService.treeList();
        return R.success(vos);
    }

    /**
     * 修改后台菜单
     *
     * @param id
     * @param menu
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> updateById(@PathVariable Integer id, @RequestBody Menu menu) {
        menuService.updateById(menu);
        return R.success("修改菜单成功");
    }

    /**
     * 修改菜单显示状态
     *
     * @param id
     * @param hidden
     * @return
     */
    @PostMapping("/updateHidden/{id}")
    public R<String> updateHidden(@PathVariable Integer id, Integer hidden) {
        menuService.updateHidden(id, hidden);
        return R.success("修改状态成功");
    }

    /**
     * 根据ID获取菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Menu> getById(@PathVariable Integer id) {
        Menu menu = menuService.getById(id);
        return R.success(menu);
    }
}
